//
//  EmailListView.m
//  SZBank
//
//  Created by LuoYao on 16/7/9.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "EmailListView.h"
#import "UserTableView.h"
#import "LYEmailTableViewCell.h"


@interface EmailListView ()<UITableViewDataSource,UITableViewDelegate>
{
    UIImageView *backImageView;
    CGFloat _cwidth,_cheight;
    NSArray *dataArray;/**< 数据源 */
}
@property (nonatomic) UITableView *tableView;
@end
@implementation EmailListView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initDataSource];
        
        self.backgroundColor = [UIColor colorWithRed:0.1615 green:0.1607 blue:0.1624 alpha:0.6];

        //backImageView
        backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH * 0.6, KHEIGHT * 0.6)];
        backImageView.image = [LocalDataManager getImageWithSourceOfPath:@"消息推送_信箱背景"];
        backImageView.center = self.center;
        backImageView.userInteractionEnabled = YES;
        [self addSubview:backImageView];
        
        
        _cwidth = backImageView.frame.size.width;
        _cheight = backImageView.frame.size.height;
        
        //清空 关闭按钮
        NSArray *imageNames = @[@"消息推送_清空",@"消息推送_关闭"];
        for (int i = 0; i < imageNames.count; i ++) {
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(backImageView.frame.size.width * 0.6 + i * backImageView.frame.size.width * 0.2, backImageView.frame.size.height * 0.9, backImageView.frame.size.width * 0.15, backImageView.frame.size.height * 0.05)];
            btn.tag = 1000 + i;
            [btn addTarget:self action:@selector(actiob_BtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [btn setImage:[LocalDataManager getImageWithSourceOfPath:imageNames[i]] forState:UIControlStateNormal];
            
            [backImageView addSubview:btn];
        }
        
        
        
        
        
        
        //表格
        
        NSArray *titleArray = @[@"发信人",@"标题",@"状态",@"时间"];
        
//        for (int i = 0; i < 3; i ++) {
//            
//            if (i < 3 - 1) {
//                
//                //横线 2行  4列
//                UILabel *rowline = [[UILabel alloc] initWithFrame:CGRectMake(0, backImageView.frame.size.height * 0.1 + i * 50, backImageView.frame.size.width, 1)];
//                rowline.backgroundColor = [UIColor redColor];
//                [backImageView addSubview:rowline];
//                
//                //单元格宽度
//                CGFloat rowWidth = backImageView.frame.size.width / 4;
//                for (int j = 0; j < 5; j ++) {
//                    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(j * rowWidth, CGRectGetMaxY(rowline.frame), 1, 50)];
//                    line .backgroundColor = [UIColor redColor];
//                    [backImageView addSubview:line];
//                    
//                    
//                    if (j < 5 - 1 && i == 0) {
//                        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(j * rowWidth, CGRectGetMaxY(rowline.frame), rowWidth, 50)];
//                        label.font = [UIFont systemFontOfSize:FONT_SIZE];
//                        label.backgroundColor = [UIColor colorWithRed:0.9555 green:0.9555 blue:0.9555 alpha:1.0];
//                        label.textAlignment = NSTextAlignmentCenter;
//                        label.text = titleArray[j];
//                        [backImageView addSubview:label];
//                    }
//                }
//        }
//            
//            //显示完成
//            if (i == 3 - 1) {
//                
//                
//                
//                //单元格宽度
//                CGFloat rowWidth = backImageView.frame.size.width / 4;
//                for (int j = 0; j < 5; j ++) {
//                    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(j * rowWidth,  backImageView.frame.size.height * 0.1 + i * 50, 1, backImageView.frame.size.height * 0.5)];
//                    line .backgroundColor = [UIColor redColor];
//                    [backImageView addSubview:line];
//                }
//                
//                //横线 2行  4列
//                UILabel *rowline = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(backImageView.subviews.lastObject.frame), backImageView.frame.size.width, 1)];
//                rowline.backgroundColor = [UIColor redColor];
//                [backImageView addSubview:rowline];
//            }
//  
//        }
        
        
        //title
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _cwidth * 0.3, _cheight * 0.1)];
        titleLabel.center = CGPointMake(_cwidth * 0.5, _cheight * 0.1);
        titleLabel.text = @"我的信箱";
        
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:FONT_SIZE];
        
        [backImageView addSubview:titleLabel];
        
        //表头
        
        for (int j = 0; j < 2; j ++) {

            UILabel *lineV = [[UILabel alloc] initWithFrame:CGRectMake(_cwidth * 0.01,_cheight * 0.2 + j * _cheight * 0.05 - 1, _cwidth * 0.97, 1)];
            lineV.backgroundColor = [UIColor blackColor];
            [backImageView addSubview:lineV];
            
            for (int i = 0; i <= titleArray.count; i ++) {
                UILabel *lineH = [[UILabel alloc] initWithFrame:CGRectMake(_cwidth * 0.01 + i  * (_cwidth * 0.97 / 4), _cheight * 0.2, 1, _cheight * 0.05)];
                lineH.backgroundColor = [UIColor blackColor];
                [backImageView addSubview:lineH];
                
                //表格内的值
                UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(_cwidth * 0.01 + i  * (_cwidth * 0.97 / 4),_cheight * 0.2, _cwidth * 0.97 / 4, _cheight * 0.05 - 1)];
                titleLabel.textAlignment = NSTextAlignmentCenter;
                
                if (i < titleArray.count) {
                    titleLabel.text = titleArray[i];
                }
                [backImageView addSubview:titleLabel];
            }
        }
        
        
        
        
        //短信展示
        [backImageView addSubview: self.tableView];
        
        [self showViewAnimation];
}
    return self;
}

/**< 初始化数据源 */
- (void)initDataSource{
   
    [NetWorkingDataManager getEmailWithSuccess:^(id sender) {
        dataArray = sender[@"updateList"];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } failure:^(id sender) {
        
    }];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //获取在视图上的点击区域
    CGPoint touchPoint = [[touches anyObject] locationInView:self];
    CGRect rect = self.subviews[0].frame;
    
    //判断包含
    if (!CGRectContainsPoint(rect, touchPoint)) {
        [self dismissViewAnimation];
    }
    
}

#pragma mark - 动画方法
- (void)showViewAnimation {
    CGAffineTransform transform = CGAffineTransformIdentity;
    transform = CGAffineTransformRotate(transform, M_PI_4);
    transform = CGAffineTransformTranslate(transform, -self.bounds.size.width / 2, -self.bounds.size.width / 2);
    [backImageView setTransform:transform];
    [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        [backImageView setTransform:CGAffineTransformIdentity];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)dismissViewAnimation {
    
    CGAffineTransform transform = CGAffineTransformIdentity;
    transform = CGAffineTransformRotate(transform, -M_PI_4);
    transform = CGAffineTransformTranslate(transform, -self.bounds.size.width / 2, self.bounds.size.width / 2);
    [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        [backImageView setTransform:transform];
    } completion:^(BOOL finished) {
        [backImageView setAlpha:0.7];
        backImageView = nil;

        [self removeFromSuperview];
    }];
}

#pragma mark - uitableView Delegate
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LYEmailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"emailcell" forIndexPath:indexPath];
    cell.sendPersonLabel.text = dataArray[indexPath.row][@"USERNAME"];
    cell.emailTitleLabel.text = dataArray[indexPath.row][@"TITLE"];
    cell.emailTypeLabel.text = [dataArray[indexPath.row][@"ISDELETE"] isEqualToString:@"0"] ? @"已读":@"未读";
    cell.sendTimeLabel.text = dataArray[indexPath.row][@"UPDATEDATE"];
    
    return cell;
}


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return _cheight * 0.1;
}
#pragma mark - 点击事件
- (void)actiob_BtnClick:(UIButton *)sender{
    switch (sender.tag - 1000) {
        case 0:
        {   //清空
            [self.tableView removeFromSuperview];
        }
            break;
        
        case 1:
        {   //关闭
            [self dismissViewAnimation];
        }
            break;
        default:
            break;
    }
}


#pragma mark - getter
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(_cwidth * 0.01, _cheight * 0.25, _cwidth * 0.98, _cheight * 0.6)];
//        _tableView.backgroundColor = [UIColor redColor];
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.tableHeaderView = [[UIView alloc] init];
    
        [_tableView registerClass:[LYEmailTableViewCell class] forCellReuseIdentifier:@"emailcell"];
    }
    return _tableView;
}
@end
